// X轴设置
import React, { useState } from 'react';
import { Switch, InputNumber, Input } from 'antd';
import Editor from 'src/pages/AnalyseManage/EditReport/components/EditorStyleType/Editor';
import FormItem from 'antd/lib/form/FormItem';
// import { filterObjectNullKeys } from 'src/libs/utils';

interface IProps {
	config: any;
	onValuesChange: ( chartType: string, key: string, values: Object, configType: string ) => void;
	form: any;
}

const XAxis = ( props: IProps ) => {
	const { config, onValuesChange, form } = props;
	const [ isShowXLineName, setIsShowXLineName ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowXLineName' ) ? config.style.isShowXLineName : false
	);
	const [ isShowXLine, setIsShowXLine ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowXLine' ) ? config.style.isShowXLine : true
	);

	const fontConfig = [ 'fontFamily', 'fontSize', 'fontColor', 'fontBold', 'fontItalic' ];

	const onChange = ( checked: boolean, name: string, cb: any ) => {
		// setIsShowXLineName( checked );
		cb && cb();
		form.setFieldsValue( { [name]: checked } );
		onValuesChange( config.type, config.key, { [name]: checked }, 'style' );
	};

	const onBlur = ( value: any, name: string ) => {
		onValuesChange( config.type, config.key, { [name]: form.getFieldsValue()[name] }, 'style' );
	};

	return (
		<div>
			<FormItem label="是否显示轴线" name="isShowXLine" labelAlign="left" labelCol={{ span: 10 }} wrapperCol={{ span: 14 }}>
				<Switch
					checked={isShowXLine}
					onChange={( checked ) => onChange( checked, 'isShowXLine', setIsShowXLine( checked ) )}
				></Switch>
			</FormItem>
			{isShowXLine && (
				<>
					<FormItem label="是否显示轴名称" name="isShowXLineName" labelAlign="left" labelCol={{ span: 10 }} wrapperCol={{ span: 14 }}>
						<Switch
							checked={isShowXLineName}
							onChange={( checked ) => onChange( checked, 'isShowXLineName', setIsShowXLineName( checked ) )}
						></Switch>
					</FormItem>

					{isShowXLineName && (
						<>
							<FormItem label="X轴名称" name="chartXAxisName">
								<Input
									onBlur={( value ) => onBlur( value, 'chartXAxisName' )}
									onPressEnter={( value ) => onBlur( value, 'chartXAxisName' )}
								></Input>
							</FormItem>
							<FormItem label="字体" name="chartXLineFont">
								<Editor
									config={config}
									form={form}
									onValuesChange={onValuesChange}
									fontFamilyName="XLineFontFamily"
									fontSizeName="XLineFontSize"
									fontColorName="XLineFontColor"
									boldName="XLineFontBold"
									italicName="XLineFontItalic"
									fontConfig={fontConfig}
								/>
							</FormItem>
						</>
					)}
					<FormItem label="轴标签倾斜度(-90至90)" name="chartXAxisLabelTilt">
						<InputNumber min={-90} max={90}></InputNumber>
					</FormItem>
				</>
			)}
		</div>
	);
};

export default React.memo( XAxis );
